package byj.com.meishi;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.CursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import java.util.List;

import byj.com.meishi.bean.Shoucang;
import byj.com.meishi.dao.ShouCangDao;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2016/9/22 0022.
 */
public class ShoucangActivity extends AppCompatActivity{

    private ListView lv;
    private SimpleCursorAdapter simpleCursorAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.shoucang_layout);
        lv = (ListView) findViewById(R.id.shoucang_lv);
        final ShouCangDao dao = new ShouCangDao(ShoucangActivity.this);
        final Cursor cursor = dao.select("SELECT * FROM collect", null);
        simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.colect_item, cursor, new String[]{"caption"},
                new int[]{R.id.tv_colect_item}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        lv.setAdapter(simpleCursorAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                final String caption = cursor.getString(cursor.getColumnIndex("caption"));

                BmobQuery<Shoucang> bmobQuery = new BmobQuery<>();
                bmobQuery.addWhereLessThanOrEqualTo("name",caption);
                bmobQuery.findObjects(new FindListener<Shoucang>() {
                    @Override
                    public void done(List<Shoucang> list, BmobException e) {
                        if(e==null){
                            Shoucang shoucang = list.get(0);
                            String url = shoucang.getUrl();
                            Intent intent = new Intent(ShoucangActivity.this, MovieActivity.class);

                            intent.putExtra("video",url);
                            intent.putExtra("caption",caption);
                            startActivity(intent);
                        }else {

                        }
                    }
                });

           }
        });
//        ShouCangDao dao = new ShouCangDao(ShoucangActivity.this);

        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                final String caption = cursor.getString(cursor.getColumnIndex("caption"));
                int collect = dao.delete("collect", "caption = ?", new String[]{caption});
                cursor.close();
                if (collect > 0){
                    Cursor select = dao.select("select * from collect", null);
                    simpleCursorAdapter.swapCursor(select);
                    simpleCursorAdapter.notifyDataSetChanged();
                }
                final Shoucang shoucang = new Shoucang();
                String objectId = "";
                BmobQuery<Shoucang> bmobQuery = new BmobQuery<Shoucang>();
                bmobQuery.addWhereContains("name",caption);
                bmobQuery.findObjects(new FindListener<Shoucang>() {
                    @Override
                    public void done(List<Shoucang> list, BmobException e) {
                        String objectId = list.get(0).getObjectId();
                        shoucang.delete(objectId, new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                if (e==null){
                                    Toast.makeText(ShoucangActivity.this, "成功", Toast.LENGTH_SHORT).show();
                                }else {
                                    Toast.makeText(ShoucangActivity.this, "失败", Toast.LENGTH_SHORT).show();
                                }

                            }
                        });
                    }
                });

                return true;
            }
        });

    }
}
